<template>
  <div class="WorkingSchedule" v-loading="loading">
    <el-form ref="form" :model="form" label-position="top" class="myform">
      <div>
        <el-form-item label="加工单号/采购单:" class="myform-item">
          <el-input
            v-model="form.processing_code"
            placeholder="请输入加工单号或采购单"
            @keyup.enter.native="onSearch"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="SKU:" class="myform-item">
          <el-input
            v-model="form.sku_code"
            placeholder="请输入SKU"
            @keyup.enter.native="onSearch"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="工厂名:" class="myform-item">
          <el-input
            v-model="form.factory_name"
            placeholder="请输入工厂名"
            @keyup.enter.native="onSearch"
            clearable
          ></el-input>
        </el-form-item>

        <el-form-item label="RFID生成状态:" class="myform-item">
          <el-select
            v-model="form.is_create"
            placeholder="请选择RFID生成状态"
            clearable
          >
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </div>
      <div>
        <el-form-item style="margin-top: 50px">
          <el-button type="primary" @click="onSearch">查询</el-button>
          <!-- <el-button type="primary" @click="onAdd">新增面料</el-button> -->
        </el-form-item>
      </div>
    </el-form>
    <div style="margin: 20px 0">
      <el-button type="primary" @click="onUpdate">更新成品加工单</el-button>
      <span style="margin-left: 30px">最后更新日期：{{ savetime }}</span>
    </div>
    <el-table
      ref="multipleTable"
      :data="tableData"
      style="width: 100%"
      border
      size="mini"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55"> </el-table-column>
      <el-table-column label="RFID生成状态" align="center" prop="is_create">
        <template slot-scope="{ row }">
          {{
            row.is_create == 0
              ? "未设置"
              : row.is_create == 1
              ? "已生成"
              : "无需生成"
          }}
        </template>
      </el-table-column>
      <el-table-column
        label="成品加工单编码"
        align="center"
        prop="processing_code"
        width="140"
      >
        <template slot-scope="{ row }">
          <div>
            <div>{{ row.processing_code }}</div>
            <div
              style="padding: 4px; color: #ffffff"
              :style="{ background: row.f_status == 0 ? 'red' : '#67C23A' }"
            >
              {{ row.f_status == 0 ? "进行中" : "已完成" }}
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="巨益采购单号" align="center" prop="jy_number">
      </el-table-column>
      <el-table-column label="聚水潭采购单号" align="center" prop="po_id" fixed>
      </el-table-column>
      <el-table-column label="款号" align="center" prop="type_no" width="140">
      </el-table-column>
      <el-table-column
        label="大货核价编码"
        align="center"
        prop="verify_price_number"
      >
      </el-table-column>
      <el-table-column label="SKU" align="center" prop="sku_code" width="140">
      </el-table-column>
      <el-table-column label="状态" align="center" prop="s_status">
        <template slot-scope="{ row }">
          {{
            row.s_status == 1
              ? "已审核"
              : row.s_status == 0
              ? "未审核"
              : row.s_status == 2
              ? "已结案"
              : row.s_status == 3
              ? "已同步"
              : ""
          }}
        </template>
      </el-table-column>
      <el-table-column
        label="工厂合同号"
        align="center"
        prop="factory_contract_no"
      >
      </el-table-column>
      <el-table-column label="供应商" align="center" prop="factory_name">
      </el-table-column>
      <el-table-column label="件数" align="center" prop="num">
      </el-table-column>
      <el-table-column label="入库数" align="center" prop="qty">
        <el-table-column label="未入库数" align="center">
          <template slot-scope="{ row }">
            {{ row.num - row.qty }}
          </template>
        </el-table-column>
      </el-table-column>
      <el-table-column label="总价" align="center" prop="countprice">
      </el-table-column>
      <el-table-column label="预付款" align="center" prop="advance_charge">
      </el-table-column>
      <el-table-column
        label="绣花厂单价"
        align="center"
        prop="embroidery_price"
      >
      </el-table-column>
      <el-table-column label="合同交期" align="center" prop="contract_date">
      </el-table-column>
      <el-table-column
        label="生产回复交期"
        align="center"
        prop="production_date"
      >
      </el-table-column>
      <el-table-column label="跟单人" align="center" prop="merchandiser">
      </el-table-column>
      <el-table-column label="理单人" align="center" prop="order_processor">
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="adduser">
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createtime"
        width="110"
      >
      </el-table-column>
      <el-table-column label="绣花厂" align="center" prop="embroidery_factory">
      </el-table-column>
      <el-table-column label="印花厂" align="center" prop="printing_factory">
      </el-table-column>
      <el-table-column label="烫金厂" align="center" prop="bronzing_factory">
      </el-table-column>
      <el-table-column
        label="印金厂"
        align="center"
        prop="gold_printing_factory"
      >
      </el-table-column>
      <el-table-column label="SKU单价" align="center" prop="unit_price">
      </el-table-column>
      <el-table-column label="客人下单日期" align="center" prop="add_date">
      </el-table-column>

      <el-table-column label="同步状态" align="center" prop="j_status">
        <template slot-scope="{ row }">
          {{ row.is_oms == 0 ? "未推送" : "已推送" }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="100">
        <template slot-scope="{ row }">
          <el-button type="text" @click="onPush(row.processing_code)"
            >推送</el-button
          >
          <el-button
            type="text"
            style="color: red"
            @click="onCancel(row.processing_code)"
            >取消</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div style="margin-top: 10px">
      <el-button
        type="primary"
        :disabled="!multipleSelection.length"
        size="default"
        @click="onCreate(1)"
        >生成RFID</el-button
      >
      <el-button
        type="primary"
        :disabled="!multipleSelection.length"
        size="default"
        @click="onCreate(2)"
        >无需生成RFID</el-button
      >
    </div>

    <pagination
      :form="form"
      :total="total"
      @paginationPostOrderTaskList="postAdminCodeGetpocessingcodelist"
    />
  </div>
</template>

<script>
import {
  postAdminCodeGetpocessingcodelist,
  postAdminCodeSetpocessingcodestatus,
  postAdminCodeSetJushuitanPurchaseUpload_shou,
  postAdminOperatesave_processing_order,
  postAdminOmsaddWarehousingProcessing,
  postAdminOmscancelProcessing,
} from "@/api";
import pagination from "@/components/pagination.vue";
export default {
  components: { pagination },
  data() {
    return {
      loading: false,
      dialogVisible: false,
      savetime: "",
      options: [
        {
          value: "0",
          label: "未设置",
        },
        {
          value: "1",
          label: "已生成",
        },
        {
          value: "2",
          label: "无需生成",
        },
      ],
      form: {
        processing_code: "",
        sku_code: "",
        is_create: "",
        factory_name: "",
        page: 1,
        size: 10,
      },

      multipleSelection: [],
      tableData: [],
      total: 1,
    };
  },
  activated() {
    let form = this.form;
    this.postAdminCodeGetpocessingcodelist(form);
  },
  methods: {
    onUpdate() {
      this.loading = true;
      let form = this.form;
      postAdminOperatesave_processing_order().then((res) => {
        this.postAdminCodeGetpocessingcodelist(form);
      });
    },
    // 取消
    onCancel(processing_code) {
      let obj = {
        processing_code,
      };
      let form = this.form;
      this.$confirm(`确定取消推送OMS?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          postAdminOmscancelProcessing(obj).then((res) => {
            this.$message({
              type: "success",
              message: "取消成功",
            });
            this.postAdminCodeGetpocessingcodelist(form);
          });
        })
        .catch(() => {
          // this.$message({
          //   type: "info",
          //   message: "已取消推送",
          // });
        });
    },
    // 推送
    onPush(processing_code) {
      let obj = {
        processing_code,
      };
      let form = this.form;
      this.$confirm(`确定推送OMS?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          postAdminOmsaddWarehousingProcessing(obj).then((res) => {
            this.$message({
              type: "success",
              message: "成功",
            });
            this.postAdminCodeGetpocessingcodelist(form);
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消推送",
          });
        });
    },
    // 生成RFID
    onCreate(e) {
      let { multipleSelection, form } = this;
      this.$confirm(`确定${e == 1 ? "生成RFID" : "无需生成RFID"}?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let obj = {
            ids: [],
            status: e,
          };
          multipleSelection.forEach((res) => {
            obj.ids.push(res.id);
          });
          postAdminCodeSetpocessingcodestatus(obj).then((res) => {
            this.$message({
              type: "success",
              message: "成功",
            });
            this.postAdminCodeGetpocessingcodelist(form);
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    // 采购列表
    async postAdminCodeGetpocessingcodelist(data) {
      this.loading = true;
      this.form = data;
      this.$refs.multipleTable.clearSelection();
      postAdminCodeGetpocessingcodelist(data)
        .then((res) => {
          this.loading = false;
          console.log("采购列表", res);
          this.savetime = res.savetime;
          this.tableData = res.data;
          this.total = Number(res.total);
        })
        .catch((err) => {
          this.loading = false;
        });
    },
    // 搜索
    onSearch() {
      this.form.page = 1;
      this.form.size = 10;
      this.postAdminCodeGetpocessingcodelist(this.form);
    },
  },
};
</script>

<style lang="scss" scoped>
.myform {
  display: flex;
  justify-content: space-between;
  > div {
    display: flex;
  }
  &-item {
    margin-right: 10px;
  }
}
.WorkingSchedule {
  background: #ffff;
  padding: 20px;
  border-radius: 2px;
  @include boxShadow;
}
</style>
